﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace QD.Tools
{
    public static class LogHelper
    {
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="txt"></param>
        public static void AddLogTxt(string txt)
        {
            try
            {
                StreamWriter writer;
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory + "Error\\";
                string requestText = System.Environment.NewLine + System.Environment.NewLine + DateTime.Now.ToString() + System.Environment.NewLine + txt;

                QD.Tools.FileHelper.CreateDirectory(baseDirectory);
                string str2 = DateTime.Now.ToString("yyyyMMdd") + ".txt";
                if (File.Exists(baseDirectory + str2))
                {
                    File.AppendAllText(baseDirectory + str2, requestText, Encoding.Unicode);                    
                }
                else
                {
                    File.WriteAllText(baseDirectory + str2, requestText, Encoding.Unicode);
                }
            }
            catch (Exception except)
            {
            }
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="txt">日志文本</param>
        /// <param name="fullFileName">文件名称</param>
        public static void AddLogTxt(string txt,string fullFileName)
        {
            try
            {
                StreamWriter writer;

                string filePath = "";
                if (fullFileName.Contains("\\"))
                {
                    filePath = fullFileName.Substring(0, fullFileName.Length - fullFileName.LastIndexOf("\\"));

                    if (!Directory.Exists(filePath))
                    {
                        QD.Tools.FileHelper.CreateDirectory(filePath);
                    }
                }
                else
                {
                    fullFileName = AppDomain.CurrentDomain.BaseDirectory + fullFileName;
                }

                if (File.Exists(fullFileName))
                {
                    File.AppendAllText(fullFileName, txt, Encoding.Unicode);
                }
                else
                {
                    File.WriteAllText(fullFileName, txt, Encoding.Unicode);
                }
            }
            catch (Exception except)
            {
            }
        }
    }
}